package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.config.common.R;
import com.itheima.reggie.entity.dto.EmployeeDto;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

/**
 * @Name EmployeeController
 * @Author BaiXin
 * @Date 2022-10-28 15:10
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 根据id查询员工回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> get(@PathVariable("id")Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);

    }

    /**
     * 修改员工信息
     * @param session
     * @param employeeDto
     * @return
     */
    @PutMapping
    public R<String> update(HttpSession session, @RequestBody EmployeeDto employeeDto) {
        //获取当前登录用户
        //Long employeeId = (Long) session.getAttribute("employee");
        //设置参数
        //employeeDto.setUpdateUser(employeeId);
        //employeeDto.setUpdateTime(LocalDateTime.now());
        //调用service方法修改
        employeeService.updateById(employeeDto);
        return R.success("修改成功");

    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        //分页构造器
        IPage<Employee> employeePage = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), Employee::getName, name);
        //排序
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(employeePage, lqw);
        return R.success(employeePage);


    }

    /**
     * 登录
     * @param session
     * @param employeeDto
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody EmployeeDto employeeDto) {

        R r = employeeService.login(employeeDto);
        if (r.getCode().equals(1)) {
            //用户正常，存入session
            Employee employee = (Employee) r.getData();
            session.setAttribute("employee", employee.getId());
        }
        return r;
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        //清空session
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param session
     * @param employeeDto
     * @return
     */
    @PostMapping
    public R add(HttpSession session, @RequestBody EmployeeDto employeeDto) {
        //判断用户名重复

      /*  Employee employee = employeeService.selectByUsername(employeeDto);
        if (employee == null) {*/
        //设置初始密码为 加密 123456
        //判断用户输入账号是否为空
        if (null == employeeDto.getUsername().trim() || "".equals(employeeDto.getUsername().trim())) {
            return R.error("账号不能为空");
        }
        employeeDto.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employeeDto.setCreateTime(LocalDateTime.now());
        //employeeDto.setUpdateTime(LocalDateTime.now());

        ////创建人，更新人
        //Long creatId = (Long) session.getAttribute("employee");
        //System.out.println(creatId);
        //employeeDto.setCreateUser(creatId);
        //employeeDto.setUpdateUser(creatId);

        employeeService.save(employeeDto);
        return R.success("新增员工成功");
      /*  }
        return R.error("用户名已存在，请重新输入");*/

    }
}
